File manager - Edit - /home/autoph/public_html/projects/AutoHub-Kiosk-Final/app/Http/Controllers/CustomerProjectionController.php
Back
<?php namespace App\Http\Controllers; require_once __DIR__.'/../../../vendor/autoload.php'; use Illuminate\Http\Request; use App\Models\Customer; use Illuminate\Support\Facades\DB; use Phpml\Regression\LeastSquares; use Illuminate\Support\Carbon; class CustomerProjectionController extends Controller { public function forecast() { $customers = DB::table('customers') ->select(DB::raw('DATE(created_at) as date'), DB::raw('count(*) as count')) ->groupBy('date') ->get(); // Fetch data from the database $customerArray = $customers->toArray(); // Create arrays of input features (dates) and output labels (customer counts) $dates = []; $counts = []; foreach ($customerArray as $row) { $dates[] = strtotime($row->date); $counts[] = $row->count; } // Split the input features and output labels into training and testing sets $splitIndex = (int) (count($dates) * 0.8); $trainingDates = array_slice($dates, 0, $splitIndex); $trainingCounts = array_slice($counts, 0, $splitIndex); $testingDates = array_slice($dates, $splitIndex); $testingCounts = array_slice($counts, $splitIndex); if (!is_array($trainingDates)) { $trainingDates = [$trainingDates]; } if (!is_array($trainingCounts)) { $trainingCounts = [$trainingCounts]; } // Create a linear regression model and train it using the training data $model = new LeastSquares(); $model->train(array_map(function ($date) { return [$date]; }, array_values($trainingDates)), array_values($trainingCounts)); // Use the model to predict the future customer counts $futureDates = [ strtotime('tomorrow'), strtotime('+1 week'), strtotime('+1 month'), strtotime('+3 months'), strtotime('+6 months'), strtotime('+1 year'), strtotime('+2 years'), strtotime('+3 years'), ]; $predictedCounts = array_map(function ($date) use ($model) { return $model->predict([$date]); }, $futureDates); return response()->json(['dates' => $futureDates, 'counts' => $predictedCounts]); } }
| ver. 1.4 |
.
| PHP 8.1.32 | Generation time: 0 |
proxy
|
phpinfo
|
Settings